/*

 */
package portalsprototype;

import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.Timer;

/**
 *
 * @author Oscar
 */
public class Screen extends JFrame {

    private static final int MIN_PORTAL_WIDTH = 330;
    private static final int SNAP_ZONE_WIDTH = 150; // should be less than MIN_PORTAL_WIDTH

    private static final int DIVIDER_WIDTH = 11;
    private int dividerPosition = 0;
    private final Portal leftPortal;
    private final Portal rightPortal;
    private final OmegaBar omegabar;

    public Screen(GraphicsConfiguration gc, OmegaBar ob) {
        super(gc);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.getContentPane().setBackground(Color.BLACK);

        JLayeredPane lp = this.getLayeredPane();

        this.omegabar = ob;

        lp.add(ob, 300);
        //ob.setBounds(200, 100, 1200, 600);
        //ob.setVisible(true);

        final Screen s = this;

        this.leftPortal = new Portal(this);
        this.add(leftPortal);
        leftPortal.setVisible(true);
        leftPortal.setDisplayTime(true);

        this.rightPortal = new Portal(this);
        this.add(rightPortal);
        rightPortal.setVisible(false);
        rightPortal.setDisplayTime(false);

        this.addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
                s.setDivisor(e.getX());
            }

            public void mouseMoved(MouseEvent e) {
            }
        });

        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mousePressed(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                checkPortalWidth();
            }

            public void mouseEntered(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        Timer t = new Timer(2000, new ActionListener() {

            private boolean flag = true;

            @Override
            public void actionPerformed(ActionEvent e) {
                leftPortal.setCurrentWindow(omegabar.getOpenWindows().getRandomWindow());
                rightPortal.setCurrentWindow(omegabar.getOpenWindows().getRandomWindow());

//                if (flag) {
//                    omegabar.setVisible(false);
//                    flag = false;
//                } else {
//                    omegabar.setVisible(true);
//                    flag = true;
//                }
                repaint();
            }
        });
        t.setInitialDelay(0);
        t.start();

    }

    @Override
    public void validate() {
        super.validate();
        int width = this.getWidth();
        int height = this.getHeight();

        if (leftPortal.isVisible() && rightPortal.isVisible()) {
            int leftWidth = dividerPosition;
            int rightPos = dividerPosition + DIVIDER_WIDTH;
            int rightWidth = width - rightPos;


            if (leftWidth < MIN_PORTAL_WIDTH || rightWidth < MIN_PORTAL_WIDTH) {
                if (leftWidth < MIN_PORTAL_WIDTH - SNAP_ZONE_WIDTH || rightWidth < MIN_PORTAL_WIDTH - SNAP_ZONE_WIDTH) {
                    this.getContentPane().setBackground(new Color(200, 100, 100));
                } else {
                    this.getContentPane().setBackground(new Color(75, 0, 0));
                }
            } else {
                this.getContentPane().setBackground(Color.BLACK);
            }
            
            leftPortal.setDisplayTime(false);
            rightPortal.setDisplayTime(true);

            leftPortal.setPosition(0, leftWidth, height);
            rightPortal.setPosition(rightPos, rightWidth, height);
        } else if (leftPortal.isVisible()) {
            leftPortal.setDisplayTime(true);
            leftPortal.setPosition(0, width, height);
        } else if (rightPortal.isVisible()) {
            rightPortal.setDisplayTime(true);
            rightPortal.setPosition(0, width, height);
        }

    }

    public void setDivisor(int x) {
        dividerPosition = x;
        this.validate();
    }

    public void split(int x) {
        if (leftPortal.isVisible() && rightPortal.isVisible()) {
            setDivisor(x);
        } else {
            dividerPosition = x;
            leftPortal.setVisible(true);
            rightPortal.setVisible(true);
            checkPortalWidth();
        }
    }

    public void checkPortalWidth() {
        int width = getWidth();
        int leftWidth = dividerPosition;
        int rightPos = dividerPosition + DIVIDER_WIDTH;
        int rightWidth = width - rightPos;

        if (leftWidth < MIN_PORTAL_WIDTH) {
            if (leftWidth < MIN_PORTAL_WIDTH - SNAP_ZONE_WIDTH && rightPortal.isVisible()) {
                leftPortal.setVisible(false);
            } else {
                dividerPosition = MIN_PORTAL_WIDTH;
            }
        } else if (rightWidth < MIN_PORTAL_WIDTH && leftPortal.isVisible()) {
            if (rightWidth < MIN_PORTAL_WIDTH - SNAP_ZONE_WIDTH) {
                rightPortal.setVisible(false);
            } else {
                dividerPosition = width - MIN_PORTAL_WIDTH - DIVIDER_WIDTH;
            }
        }

        validate();
    }

    public void hideOmegaBar() {
    }

    public void showOmegaBar() {
    }
}
